package com.test.designpattern;

/**
 * 工厂模式
 * =============================================================
 * Version          Author          Date            Description
 * 1.0             01144895        2018/2/2             创建
 * =============================================================
 */
interface Example
{

    void echo(String input);
}

class Example_0 implements Example
{

    @Override
    public void echo(String input)
    {

        System.out.println("Example_0类输出：" + input);
    }
}

class Example_1 implements Example
{

    @Override
    public void echo(String input)
    {

        System.out.println("Example_1类输出：" + input);
    }
}

public class Pattern_0
{

    //静态内部类：普通工厂模式实现
    static class ExampleFactory
    {

        public static Example getInstance(int type)
        {

            switch (type)
            {
                case 0:
                    return new Example_0();
                case 1:
                    return new Example_1();
                default:
                    return new Example_0();
            }
        }
    }

    //静态内部类：反射实现工厂模式
    static class InvokeExampleFactory
    {

        public static Object getInstance(Class<? extends Example> cl)
                throws IllegalAccessException, InstantiationException
        {

            return cl.newInstance();
        }
    }

    public static void main(String[] args) throws InstantiationException, IllegalAccessException
    {

        Example example = ExampleFactory.getInstance(5);
        example.echo("test");

        Example example1 = (Example) InvokeExampleFactory.getInstance(Example_1.class);
        example1.echo("test");
    }
}
